import { plugin, AMessage } from '../../api/api'
import {
  _path,
  Write_player,
  _dirname,
  existplayer,
  bound,
  _item,
  Write_iteam
} from '../../mode/allapi'
export class generals extends plugin {
  constructor() {
    super({
      dsc: '资源',
      rule: [
        {
          reg: /^(#|\/)?征收*$/,
          fnc: 'fenjie'
        },
        {
          reg: /^(#|\/)?驻守.*$/,
          fnc: 'garrison'
        },
        {
          reg: /^(#|\/)?取消驻守.*$/,
          fnc: 'cancel'
        }
      ]
    })
  }

  async fenjie(e: AMessage) {
    const id = e.user_id
    if (!(await existplayer(id))) return false
    const player = await (await _path(id)).player
    if (player.state == 1) return e.reply('行军中')
    const log = []
    const currentTime = new Date().getTime()
    const lastTime = player.time
    const minutes = Math.floor((currentTime - lastTime) / 60000)
    if (minutes < 5) {
      e.reply('内务府：国以民为本，社稷亦为民而立，还请主公莫要频繁征收。')
      return false
    }
    const resources = ['food', 'stone', 'wood', 'iron', 'currency']
    const resourceNames = ['粮草', '石料', '木材', '铁矿', '五铢钱']

    resources.forEach((resource, index) => {
      const production = (player[resource + '_production'] * minutes) / 60
      if (
        player[resource + '_num'] + Math.round(production) >=
        player[resource + '_max']
      ) {
        log.push(`${resourceNames[index]}已满，`)
      } else {
        player[resource + '_num'] += Math.round(production)
        log.push(`获得${Math.round(production)}${resourceNames[index]}，`)
      }
    })
    player.time = currentTime
    await Write_player(id, player)
    e.reply(log)
    return false
  }
  async garrison(e: AMessage) {
    const id = e.user_id
    if (!(await existplayer(id))) return false
    const didian = e.msg.replace(/(#|\/)?驻守/, '')
    const place = await _item(id, 'place')
    const player = await (await _path(id)).player
    const wujiang = player.wujian
    if (wujiang[4] == undefined || wujiang[4].name == undefined)
      return e.reply('阵法未配置主将，无法进入驻守')
    if (wujiang[4].hp <= 0)
    return e.reply('主将未领兵，无法进入驻守')
    const action = await bound(didian, place, 'name')
    if (action == undefined) return e.reply(`未发现名为${didian}的地点`)
    if (action.belong != id) return e.reply(`${didian}不属于主公`)
    if (player.position != didian) return e.reply(`请先前往${didian}`)
    player.garrisons = 1
    action.garrison = 1
    await Write_player(id, player)
    await Write_iteam(id, 'place', place)
    e.reply('领命驻守')
  }
  async cancel(e: AMessage) {
    const id = e.user_id
    if (!(await existplayer(id))) return false
    const didian = e.msg.replace(/(#|\/)?取消驻守/, '')
    const place = await _item(id, 'place')
    const player = await (await _path(id)).player
    const action = await bound(didian, place, 'name')
    if (action == undefined) return e.reply(`未发现名为${didian}的地点`)
    if (action.belong != id) return e.reply(`${didian}不属于主公`)
    if (player.position != didian) return e.reply(`请先前往${didian}`)
    player.garrisons = 0
    action.garrison = 0
    await Write_player(id, player)
    await Write_iteam(id, 'place', place)
    e.reply('驻军:收到')
  }
}
